<?php

namespace app\admin\model;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Record extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'record';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'startime_text',
        'endtime_text',
        'state_text',
        'status_text',
        'plan_id_text',
        'admin_text',
        'remarks_text',
        'createtime_text',
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getStartimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['startime']) ? $data['startime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getPlanIdTextAttr($value, $data){
        return Plan::where('id',$data['plan_id'])->value('title')?Plan::where('id',$data['plan_id'])->value('title'):'-';
    }

    public function getAdminTextAttr($value, $data){
        return Admin::where('id',$data['admin_id'])->value('nickname')?Admin::where('id',$data['admin_id'])->value('nickname'):'-';
    }


    public function getRemarksTextAttr($value, $data){
        return strip_tags($data['remarks']);
    }


    protected function setStartimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    //获取信息
    public function getInfo($id=null,$plan_id=null,$project_id=null,$admin_id=null,$status=null,$order="weigh desc,id desc",$limit=50,$page=1,$pageCount=10) {
        $offset = ($page-1)*$pageCount;
        $where = [];
        if($id){$where['id'] = ['=',$id];}
        if($plan_id){$where['plan_id'] = ['=',$plan_id];}
        if($project_id){$where[] = ['exp',Db::raw("FIND_IN_SET($project_id,project_id)")];}
        if($admin_id){$where['admin_id'] = ['=',$admin_id];}
        if($status||$status==="0" ){$where['status'] = ['=',$status];}
        $info = collection(self::where($where)->order($order)->limit($offset,$pageCount)->select())->toArray();
        return $info;
    }


}
